/**
 * 参数配置表管理初始化
 */
var Config = {
    id: "ConfigTable",	//表格id
    seItem: null,		//选中的条目
    table: null,
    layerIndex: -1,
    ztree: null,
    categoryId:0,
    categoryName:null
};

/**
 * 初始化表格的列
 */
Config.initColumn = function () {
    return [
        {field: 'selectItem', radio: true},
        {title: '参数名称', field: 'name', align: 'left', valign: 'middle', sortable: true},
        {title: '参数键名', field: 'paramKey', align: 'left', valign: 'middle', sortable: true},
        {title: '参数键值', field: 'paramValue', align: 'left', valign: 'middle', sortable: true},
        {title: '描述', field: 'description', align: 'left', valign: 'middle', sortable: true},
        {title: '操作', field: 'id', align: 'center', width: '120px', valign: 'middle',formatter:function(value,row,index){
    		var _html = '<a href="javascript:Config.edit(\''+row.id+'\')" title="修改"><i class="fa fa-pencil"></i></a>';
    		_html += '<a href="javascript:Config.del(\''+row.id+'\',\''+row.name+'\')" title="删除"><i class="fa fa-trash-o"></i></a>';
    		return _html;
        }}];
};

/**
 * 检查是否选中
 */
Config.check = function () {
    var selected = $('#' + this.id).bootstrapTable('getSelections');
    if(selected.length == 0){
        Sys.info("请先选中表格中的某一记录！");
        return false;
    }else{
        Config.seItem = selected[0];
        return true;
    }
};

/**
 * 点击新增参数配置表
 */
Config.add = function () {
    var index = Sys.open('新增参数配置',Sys.backendPath + '/config/to_add',['800px', '420px']);
    this.layerIndex = index;
};

/**
 * 打开查看参数配置表详情
 */
Config.edit = function (id) {
	var flag = false;
	if(id){
		flag = true;
	}else if (this.check()) {
        id = this.seItem.id;
        flag = true;
    }
	if (flag) {
    	var index = Sys.open('参数配置详情',Sys.backendPath + '/config/to_update/' + id,['800px', '420px']);
        this.layerIndex = index;
    }
};

/**
 * 删除参数配置表
 */
Config.del = function (id,name) {
	var flag = false;
	if(id){
		flag = true;
	}else if (this.check()) {
        id = this.seItem.id;
        name = this.seItem.name
        flag = true;
    }
	if (flag) {
        var operation = function(){
	        var ajax = new $ax(Sys.backendPath + "/config/delete", function (data) {
	            Sys.success("删除成功!");
	            Config.table.refresh();
	        }, function (data) {
	            Sys.error("删除失败!" + data.responseJSON.message + "!");
	        });
	        ajax.set("configId",id);
	        ajax.start();
	    };
	    Sys.confirm("是否删除参数配置?",operation);
    }
};

/**
 * 查询参数配置表列表
 */
Config.search = function () {
    var queryData = {};
    queryData['categoryId'] = Config.categoryId==0?"":Config.categoryId;
    queryData['paramKey'] = $("#paramKey").val();
    queryData['paramValue'] = $("#paramValue").val();
    queryData['description'] = $("#description").val();
    Config.table.refresh({query: queryData});
};

Config.onClickConfig = function (e, treeId, treeNode) {
    Config.categoryId = treeNode.id;
    Config.categoryName = treeNode.name;
    Config.search();
};


/**
 * 点击添加参数分类
 */
Config.openAddCategory = function () {
	this.layerIndex = Sys.open('新增分类',Sys.backendPath + '/config/category/to_add',['600px', '400px']);
};

/**
 * 打开查看参数分类详情
 */
Config.openCategoryDetail = function () {
    if (Config.categoryId) {
    	this.layerIndex = Sys.open('修改分类',Sys.backendPath + '/config/category/to_update/' + Config.categoryId,['600px', '400px']);
    }else{
        Sys.info("请先选中某一分类！");
    }
};

/**
 * 删除参数分类
 */
Config.delCategory = function () {
    if (Config.categoryId) {
        var operation = function(){
	        var ajax = new $ax(Sys.backendPath + "/config/category/delete", function (data) {
	            Sys.success("删除成功!");
	            Config.ztree.init();
	            Config.categoryId=null;
	        }, function (data) {
	            Sys.error("删除失败!" + data.responseJSON.message + "!");
	        });
	        ajax.set("configCategoryId",Config.categoryId);
	        ajax.start();
	    };
	    Sys.confirm("是否删除参数分类?",operation);
    }else{
        Sys.info("请先选中某一分类！");
    }
};
$(function () {
    var defaultColunms = Config.initColumn();
    var table = new BSTable(Config.id, Sys.backendPath + "/config/list", defaultColunms);
    table.setPaginationType("server");
    table.setSubHeight(-5);
    table.setSort("weight");
    Config.table = table.init();
    var ztree = new $ZTree("categoryTree", Sys.backendPath + "/config/category/tree");
    ztree.bindOnClick(Config.onClickConfig);
    ztree.init();
    Config.ztree = ztree;
    fuzzySearch('categoryTree','#seachKey',null,true);
});
